//package com.lhb.boyixue.user.config;
//
//import com.lhb.boyixue.user.model.Question;
//import org.apache.commons.csv.CSVFormat;
//import org.apache.commons.csv.CSVPrinter;
//
//import java.io.BufferedWriter;
//import java.io.File;
//import java.io.FileWriter;
//import java.io.IOException;
//import java.util.List;
//
///**
// * @author 86131
// * @version 1.0
// * @description
// * @date 2025/5/26 下午1:18
// */
//public class SqlGenerator {
//
//    public static void generateMysqlFiles(List<Question> questions, String outputDir) throws IOException {
//        // 创建输出目录
//        File directory = new File(outputDir);
//        if (!directory.exists()) {
//            directory.mkdirs();
//        }
//
//        // 生成CSV文件
//        String csvPath = outputDir + File.separator + "questions.csv";
//        generateCsvFile(questions, csvPath);
//
//        // 生成SQL文件
//        String sqlPath = outputDir + File.separator + "questions.sql";
//        generateSqlFile(questions, sqlPath);
//    }
//
//    private static void generateCsvFile(List<Question> questions, String csvPath) throws IOException {
//        try (
//                BufferedWriter writer = new BufferedWriter(new FileWriter(csvPath));
//                CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT
//                        .withHeader("question_id", "question_text", "option_a", "option_b",
//                                "option_c", "option_d", "correct_answer", "explanation",
//                                "category", "difficulty"))
//        ) {
//            for (Question q : questions) {
//                csvPrinter.printRecord(
//                        q.getQuestionId(),
//                        q.getQuestionText(),
//                        q.getOptionA(),
//                        q.getOptionB(),
//                        q.getOptionC(),
//                        q.getOptionD(),
//                        q.getCorrectAnswer(),
//                        q.getExplanation(),
//                        q.getCategory(),
//                        q.getDifficulty()
//                );
//            }
//
//            csvPrinter.flush();
//        }
//
//        System.out.println("CSV文件已保存: " + csvPath);
//    }
//
//    private static void generateSqlFile(List<Question> questions, String sqlPath) throws IOException {
//        try (BufferedWriter writer = new BufferedWriter(new FileWriter(sqlPath))) {
//            // 创建表的SQL
//            String createTableSql = "CREATE TABLE IF NOT EXISTS `questions` (\n" +
//                    "  `id` int(11) NOT NULL AUTO_INCREMENT,\n" +
//                    "  `question_id` varchar(10) DEFAULT NULL,\n" +
//                    "  `question_text` text NOT NULL,\n" +
//                    "  `option_a` text,\n" +
//                    "  `option_b` text,\n" +
//                    "  `option_c` text,\n" +
//                    "  `option_d` text,\n" +
//                    "  `correct_answer` varchar(10) DEFAULT NULL,\n" +
//                    "  `explanation` text,\n" +
//                    "  `category` varchar(50) DEFAULT NULL,\n" +
//                    "  `difficulty` varchar(20) DEFAULT NULL,\n" +
//                    "  PRIMARY KEY (`id`)\n" +
//                    ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n\n";
//
//            writer.write(createTableSql);
//
//            // 插入数据的SQL
//            writer.write("INSERT INTO `questions` (`question_id`, `question_text`, `option_a`, `option_b`, `option_c`, `option_d`, `correct_answer`, `explanation`, `category`, `difficulty`) VALUES\n");
//
//            int count = 0;
//            for (Question q : questions) {
//                if (count > 0) {
//                    writer.write(",\n");
//                }
//
//                writer.write("('" + escapeSql(q.getQuestionId()) + "', '" + escapeSql(q.getQuestionText()) + "', ");
//                writer.write(q.getOptionA().isEmpty() ? "NULL, " : "'" + escapeSql(q.getOptionA()) + "', ");
//                writer.write(q.getOptionB().isEmpty() ? "NULL, " : "'" + escapeSql(q.getOptionB()) + "', ");
//                writer.write(q.getOptionC().isEmpty() ? "NULL, " : "'" + escapeSql(q.getOptionC()) + "', ");
//                writer.write(q.getOptionD().isEmpty() ? "NULL, " : "'" + escapeSql(q.getOptionD()) + "', ");
//                writer.write(q.getCorrectAnswer().isEmpty() ? "NULL, " : "'" + escapeSql(q.getCorrectAnswer()) + "', ");
//                writer.write(q.getExplanation().isEmpty() ? "NULL, " : "'" + escapeSql(q.getExplanation()) + "', ");
//                writer.write(q.getCategory().isEmpty() ? "NULL, " : "'" + escapeSql(q.getCategory()) + "', ");
//                writer.write(q.getDifficulty().isEmpty() ? "NULL" : "'" + escapeSql(q.getDifficulty()) + "'");
//                writer.write(")");
//
//                count++;
//            }
//
//            writer.write(";");
//        }
//
//        System.out.println("SQL文件已保存: " + sqlPath);
//    }
//
//    private static String escapeSql(String input) {
//        if (input == null) {
//            return "";
//        }
//        return input.replace("'", "\\'");
//    }
//}
